#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "HuJiang";

import re;
# from base import python_base_02 as pb02;
from base.python_base_02 import *;
import os;
from util.pymysql_util import *;
from datetime import datetime;
import json;
import hashlib;

'''
python 基础练习
'''

# ======== 模块调用 ========
def fun_10():
    # pb02.fun_01();
    print(User("hj", 22, "hj@163.com").__dict__);

# ======== 异常处理用 ========
def fun_11():
    try:
        for i in list(range(1, 10)):
            print(i);
        a = 1 / 1;
    except BaseException as be:
        print(be);
    finally:
        print("必须执行的代码块");

# ======== 文件读写 ========
def fun_12():
    f = open(file="D:/temp/text.txt", mode="w", encoding="utf-8");
    f.writelines(["cdacda", "\n", "cadscdasdc", "\n", "传达出大事传达室成都市擦", "\n", "cdsacdsacdas"]);
    f.close();

def fun_13():
    f =None;
    try:
        f = open(file="D:/temp/text.txt", mode="r", encoding="utf-8");
        results = f.readlines();
        print(results);
    except BaseException as e:
        print(e);
    finally:
        if f:
            f.close();

def fun_14():
    with open(file="D:/temp/text.txt", mode="r", encoding="utf-8") as f:
        print(f.readlines());

def fun_15():
    with open(file="D:/temp/text.txt", mode="a", encoding="utf-8") as f:
        f.writelines(["追加第一行", "\n", "追加第二行", "\n", "追加第三行"]);

# ======== OS操作 ========
def fun_16():
    print(os.name);
    dest_folder = "D:/temp/aa/bb";
    if not os.path.exists(dest_folder):
        # os.mkdir(dest_folder);
        os.makedirs(dest_folder);
    os.rename("D:/temp/text.txt", "D:/temp/text1.txt");

# ======== mysql操作 ========
def fun_17():
    # sql 语句
    insert_sql = insert_sql = 'insert into user (user_name, password, create_date) ' \
                'values("%s", "%s", "%s")' % ("hj2", "111111", datetime.now().strftime("%Y-%m-%d %H:%M:%S"));
    query_sql = "select * from user";

    connection, cursor = None, None;
    try:
        # 获取链接对象、游标对象
        connection, cursor = get_connection_cursor();
        # 执行新增操作
        num = execute_(insert_sql, cursor);
        print(num);
        # 提交事务
        commit_(connection);

        # 执行查询操作
        result = query_(query_sql, cursor);
        print(result);
    except BaseException as e:
        print(e);
        # 回滚事务
        rollback_(connection);
    finally:
        # 关闭连接和游标
        close_(connection, cursor);

# ======== json ========
def fun_18():
    # json 字符串转 dict
    j = '{"name":"hj", "age":19, "gender":"male"}';
    print(j);
    print(json.loads(j));
    # dict 转 json
    d = {"color":"red", "name":"table", "price":34.45, "count":33};
    print(d);
    print(json.dumps(d));
    # 对象转 json 字符串
    user = User(user_name="hahhh", password="111111", email="hj@163.com");
    print(json.dumps(user.__dict__));
    # json 字符串转对象
    j = '{"user_name": "sss", "password": "2222", "email": "aa@163.com"}';
    user.__dict__ = json.loads(j);
    print(user.__dict__);

# ======== datetime ========
def fun_19():
    print(datetime.now());
    print(datetime.now().timestamp());
    print(datetime.now().strftime("%Y/%m/%d %H:%M:%S"));
    print(datetime.strptime("1860年5月11日", "%Y年%m月%d日"));

# ======== hashlib ========
def fun_20(password):
    password = password + "salthj";
    print(hashlib.md5(password.encode("utf-8")).hexdigest());

if __name__ == '__main__':
    fun_19();

